package com.ijustyce.denoise.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.ijustyce.denoise.R;
import com.ijustyce.denoise.SettleDoc.MyFile;

import java.util.List;

/**
 * Created by Administrator on 2018/2/25 0025.
 */

public class MyFileAdapter extends RecyclerView.Adapter<MyFileAdapter.ViewHolder> {
    private List<MyFile> myFileList;
    private Context mContext;

    public MyFileAdapter(List<MyFile> list) {
        super();
        myFileList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_layout, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
       MyFile myFile = myFileList.get(position);
        holder.myFileName.setText(myFile.getFileName());
        holder.myFileImage.setImageResource(myFile.getImageId());
        holder.myFileSize.setText(myFile.getFileSize());
        holder.myFilePower.setText(myFile.getFilePower());
        holder.myFileDate.setText(myFile.getFileDate());
    }

    @Override
    public int getItemCount() {
        return myFileList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView myFileName;
        ImageView myFileImage;
        TextView myFileSize;
        TextView myFilePower;
        TextView myFileDate;

        public ViewHolder(View itemView) {
            super(itemView);
            myFileName = itemView.findViewById(R.id.myFileName);
            myFileImage = itemView.findViewById(R.id.myFileImage);
            myFileSize = itemView.findViewById(R.id.fileSize);
            myFilePower = itemView.findViewById(R.id.filePower);
            myFileDate = itemView.findViewById(R.id.fileDate);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //可以选择直接在本位置直接写业务处理
                    //Toast.makeText(context,"点击了xxx",Toast.LENGTH_SHORT).show();
                    //此处回传点击监听事件
                    if(onItemClickListener!=null){
                        onItemClickListener.OnItemClick(myFileList.get(getLayoutPosition()));
                    }
                }
            });
        }

    }

    /**
     * 设置item的监听事件的接口
     */
    public interface OnItemClickListener {
        /**
         * 接口中的点击每一项的实现方法，参数自己定义
         *
         * @param data 点击的item的数据
         */
        public void OnItemClick(MyFile data);
    }

    //需要外部访问，所以需要设置set方法，方便调用
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}

